<?php

/**
 * @author 	Samuel Carlier
 * @package	P3Base_Iterator
 */
abstract class P3Base_Iterator_Simple implements Iterator {

	/**
	 * @var array
	 */
	protected $_array;
	
	/**
	 * @var int
	 */
	protected $_key;

	/**
	 * @var bool
	 */
	protected $_valid = true;

	/**
	 * @param array $array
	 */
	public function __construct(array $array) {
		$this->_array = $array;
		$this->rewind();
	}

	/**
	 * 
	 */
	public function rewind() {
		reset($this->_array);

		$this->_key = 0;
		$this->_valid = true;

		if(count($this->_array) === 0) {
			$this->_valid = false;
		}
	}

	/**
	 * @return int
	 */
	public function key() {
		return $this->_key;
	}

	/**
	 * 
	 */
	public function next() {
		$this->_key++;

		if(next($this->_array) === false) {
			$this->_valid = false;
		}
	}

	/**
	 * @return bool
	 */
	public function valid() {
		return $this->_valid;
	}
}
?>
